package review.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import review.config.Constants;
import review.entity.Menu;
import review.system.service.MenuService;

@Controller
@Api(tags = {"菜单操作接口"})
public class MenuAction {
	
	@Autowired
	private MenuService menuService;

	@ApiOperation(value = "菜单JSON", notes = "", httpMethod = "GET")
	@RequestMapping(value="/menu",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getMenu() {
		
		List<Menu> menus = menuService.findAll();
		return menuService.transformJSON(menus);
	}
	
	@ApiOperation(value = "添加或更新菜单", notes = "", httpMethod = "POST")
	@RequestMapping(value="/menu/save",produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object saveMenu(Menu menus) {
		JSONObject object = new JSONObject();
		try {
			menuService.addMenuItem(menus);
			object.put(Constants.SUCCESS, true);
		} catch (Exception e) {
			object.put(Constants.SUCCESS, false);
			object.put(Constants.MESSAGE, e.getMessage());
		}
		return object;
	}
}
